package com.exporter.utility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;

public class DateComparator implements Comparator<String>
{
	String sortType;

	public DateComparator(String sortType)
	{
		this.sortType = sortType;
	}

	@Override
	public int compare(String date1, String date2)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM, yyyy");
		Date firstDate = null;
		Date secondDate = null;
		try
		{
			firstDate = sdf.parse(date1);
			secondDate = sdf.parse(date2);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(firstDate);
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(secondDate);
		if (cal1.after(cal2))
		{
			if ("asc".equals(sortType))
			{
				return 1;
			}
			else if ("dsc".equals(sortType))
			{
				return -1;
			}
			else
			{
				return 0;
			}
		}
		else if (cal1.before(cal2))
		{
			if ("asc".equals(sortType))
			{
				return -1;
			}
			else if ("dsc".equals(sortType))
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}

	}

}
